package at.jku.pervasive;

import java.util.Date;

public class Measurement
{

	private String description;
	private Date endDate;
	private Position[] positions;
	private Date startDate;
	private long duration;

	private String leftTagId;
	private String rightTagId;
	private String frontTagId;
	private String backTagId;

	public String getLeftTagId()
	{
		return leftTagId;
	}

	public void setLeftTagId(String leftTagId)
	{
		this.leftTagId = leftTagId;
	}

	public String getRightTagId()
	{
		return rightTagId;
	}

	public void setRightTagId(String rightTagId)
	{
		this.rightTagId = rightTagId;
	}

	public String getFrontTagId()
	{
		return frontTagId;
	}

	public void setFrontTagId(String chestTagId)
	{
		this.frontTagId = chestTagId;
	}

	public String getBackTagId()
	{
		return backTagId;
	}

	public void setBackTagId(String backTagId)
	{
		this.backTagId = backTagId;
	}

	public Measurement()
	{
		super();
	}

	public Measurement(String description, Date startTime, Date endTime)
	{
		this();

		this.description = description;
		this.startDate = startTime;
		this.endDate = endTime;

		this.duration = this.endDate.getTime() - this.startDate.getTime();
	}

	public long getDuration()
	{
		if (duration == 0L && startDate != null && endDate != null)
		{
			duration = endDate.getTime() - startDate.getTime();
		}
		return duration;
	}

	/**
	 * @return Returns the description.
	 */
	public String getDescription()
	{
		return description;
	}

	/**
	 * @return Returns the endDate.
	 */
	public Date getEndDate()
	{
		return endDate;
	}

	/**
	 * @return Returns the positions.
	 */
	public Position[] getPositions()
	{
		return positions;
	}

	/**
	 * @return Returns the startDate.
	 */
	public Date getStartDate()
	{
		return startDate;
	}

	/**
	 * @param positions The positions to set.
	 */
	public void setPositions(Position[] positions)
	{
		this.positions = positions;
	}

	@Override
	public String toString()
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("measurement: ").append(this.description).append(Helper.LINE_SEPARATOR);
		stringBuilder.append("start date: ").append(Helper.formatDate(this.startDate));
		stringBuilder.append(" (").append(this.getDuration()).append("ms)").append(Helper.LINE_SEPARATOR);

		if (this.positions != null && this.positions.length > 0)
		{
			int count = this.positions.length;
			stringBuilder.append(count).append(" position").append((count > 1) ? "s " : " ");
			stringBuilder.append("<").append(this.positions[0]).append(">");
			if (count > 2)
				stringBuilder.append(" , ...");
			if (count != 1)
				stringBuilder.append(" , <").append(this.positions[count - 1]).append(">");
		}
		else
		{
			stringBuilder.append("no positions for this measurement");
		}
		return stringBuilder.toString();
	}
}
